2 class MWRestrictionsTest
extends PHPUnit_Framework_TestCase
{
4 use MediaWikiCoversValidator
;
6 protected static $restrictionsForChecks;
8 public static function setUpBeforeClass() {
9 self
::$restrictionsForChecks = MWRestrictions
::newFromArray( [
19 * @covers MWRestrictions::newDefault
20 * @covers MWRestrictions::__construct
22 public function testNewDefault() {
23 $ret = MWRestrictions
::newDefault();
24 $this->assertInstanceOf( 'MWRestrictions', $ret );
26 '{"IPAddresses":["0.0.0.0/0","::/0"]}',
32 * @covers MWRestrictions::newFromArray
33 * @covers MWRestrictions::__construct
34 * @covers MWRestrictions::loadFromArray
35 * @covers MWRestrictions::toArray
36 * @dataProvider provideArray
38 * @param bool|InvalidArgumentException $expect True if the call succeeds,
39 * otherwise the exception that should be thrown.
41 public function testArray( $data, $expect ) {
42 if ( $expect === true ) {
43 $ret = MWRestrictions
::newFromArray( $data );
44 $this->assertInstanceOf( 'MWRestrictions', $ret );
45 $this->assertSame( $data, $ret->toArray() );
48 MWRestrictions
::newFromArray( $data );
49 $this->fail( 'Expected exception not thrown' );
50 } catch ( InvalidArgumentException
$ex ) {
51 $this->assertEquals( $expect, $ex );
56 public static function provideArray() {
58 [ [ 'IPAddresses' => [] ], true ],
59 [ [ 'IPAddresses' => [ '127.0.0.1/32' ] ], true ],
61 [ 'IPAddresses' => [ '256.0.0.1/32' ] ],
62 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
65 [ 'IPAddresses' => '127.0.0.1/32' ],
66 new InvalidArgumentException( 'IPAddresses is not an array' )
70 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
73 [ 'foo' => 'bar', 'bar' => 42 ],
74 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
80 * @covers MWRestrictions::newFromJson
81 * @covers MWRestrictions::__construct
82 * @covers MWRestrictions::loadFromArray
83 * @covers MWRestrictions::toJson
84 * @covers MWRestrictions::__toString
85 * @dataProvider provideJson
87 * @param array|InvalidArgumentException $expect
89 public function testJson( $json, $expect ) {
90 if ( is_array( $expect ) ) {
91 $ret = MWRestrictions
::newFromJson( $json );
92 $this->assertInstanceOf( 'MWRestrictions', $ret );
93 $this->assertSame( $expect, $ret->toArray() );
95 $this->assertSame( $json, $ret->toJson( false ) );
96 $this->assertSame( $json, (string)$ret );
99 FormatJson
::encode( $expect, true, FormatJson
::ALL_OK
),
104 MWRestrictions
::newFromJson( $json );
105 $this->fail( 'Expected exception not thrown' );
106 } catch ( InvalidArgumentException
$ex ) {
107 $this->assertTrue( true );
112 public static function provideJson() {
115 '{"IPAddresses":[]}',
116 [ 'IPAddresses' => [] ]
119 '{"IPAddresses":["127.0.0.1/32"]}',
120 [ 'IPAddresses' => [ '127.0.0.1/32' ] ]
123 '{"IPAddresses":["256.0.0.1/32"]}',
124 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
127 '{"IPAddresses":"127.0.0.1/32"}',
128 new InvalidArgumentException( 'IPAddresses is not an array' )
132 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
135 '{"foo":"bar","bar":42}',
136 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
140 new InvalidArgumentException( 'Invalid restrictions JSON' )
144 new InvalidArgumentException( 'Invalid restrictions JSON' )
150 * @covers MWRestrictions::checkIP
151 * @dataProvider provideCheckIP
155 public function testCheckIP( $ip, $pass ) {
156 $this->assertSame( $pass, self
::$restrictionsForChecks->checkIP( $ip ) );
159 public static function provideCheckIP() {
161 [ '10.0.0.1', true ],
162 [ '172.16.0.0', true ],
163 [ '192.0.2.1', false ],
164 [ '2001:db8:1::', true ],
165 [ '2001:0db8:0000:0000:0000:0000:0000:0000', true ],
166 [ '2001:0DB8:8000::', false ],
171 * @covers MWRestrictions::check
172 * @dataProvider provideCheck
173 * @param WebRequest $request
174 * @param Status $expect
176 public function testCheck( $request, $expect ) {
177 $this->assertEquals( $expect, self
::$restrictionsForChecks->check( $request ) );
180 public function provideCheck() {
183 $mockBuilder = $this->getMockBuilder( 'FauxRequest' )
184 ->setMethods( [ 'getIP' ] );
186 foreach ( self
::provideCheckIP() as $checkIP ) {
188 $request = $mockBuilder->getMock();
190 $request->expects( $this->any() )->method( 'getIP' )
191 ->will( $this->returnValue( $checkIP[0] ) );
192 $ok['ip'] = $checkIP[1];
194 /* If we ever add more restrictions, add nested for loops here:
195 * foreach ( self::provideCheckFoo() as $checkFoo ) {
196 * $request->expects( $this->any() )->method( 'getFoo' )
197 * ->will( $this->returnValue( $checkFoo[0] );
198 * $ok['foo'] = $checkFoo[1];
200 * foreach ( self::provideCheckBar() as $checkBar ) {
201 * $request->expects( $this->any() )->method( 'getBar' )
202 * ->will( $this->returnValue( $checkBar[0] );
203 * $ok['bar'] = $checkBar[1];
210 $status = Status
::newGood();
211 $status->setResult( $ok === array_filter( $ok ), $ok );
212 $ret[] = [ $request, $status ];