2 class MWRestrictionsTest
extends PHPUnit_Framework_TestCase
{
4 protected static $restrictionsForChecks;
6 public static function setUpBeforeClass() {
7 self
::$restrictionsForChecks = MWRestrictions
::newFromArray( [
17 * @covers MWRestrictions::newDefault
18 * @covers MWRestrictions::__construct
20 public function testNewDefault() {
21 $ret = MWRestrictions
::newDefault();
22 $this->assertInstanceOf( 'MWRestrictions', $ret );
24 '{"IPAddresses":["0.0.0.0/0","::/0"]}',
30 * @covers MWRestrictions::newFromArray
31 * @covers MWRestrictions::__construct
32 * @covers MWRestrictions::loadFromArray
33 * @covers MWRestrictions::toArray
34 * @dataProvider provideArray
36 * @param bool|InvalidArgumentException $expect True if the call succeeds,
37 * otherwise the exception that should be thrown.
39 public function testArray( $data, $expect ) {
40 if ( $expect === true ) {
41 $ret = MWRestrictions
::newFromArray( $data );
42 $this->assertInstanceOf( 'MWRestrictions', $ret );
43 $this->assertSame( $data, $ret->toArray() );
46 MWRestrictions
::newFromArray( $data );
47 $this->fail( 'Expected exception not thrown' );
48 } catch ( InvalidArgumentException
$ex ) {
49 $this->assertEquals( $expect, $ex );
54 public static function provideArray() {
56 [ [ 'IPAddresses' => [] ], true ],
57 [ [ 'IPAddresses' => [ '127.0.0.1/32' ] ], true ],
59 [ 'IPAddresses' => [ '256.0.0.1/32' ] ],
60 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
63 [ 'IPAddresses' => '127.0.0.1/32' ],
64 new InvalidArgumentException( 'IPAddresses is not an array' )
68 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
71 [ 'foo' => 'bar', 'bar' => 42 ],
72 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
78 * @covers MWRestrictions::newFromJson
79 * @covers MWRestrictions::__construct
80 * @covers MWRestrictions::loadFromArray
81 * @covers MWRestrictions::toJson
82 * @covers MWRestrictions::__toString
83 * @dataProvider provideJson
85 * @param array|InvalidArgumentException $expect
87 public function testJson( $json, $expect ) {
88 if ( is_array( $expect ) ) {
89 $ret = MWRestrictions
::newFromJson( $json );
90 $this->assertInstanceOf( 'MWRestrictions', $ret );
91 $this->assertSame( $expect, $ret->toArray() );
93 $this->assertSame( $json, $ret->toJson( false ) );
94 $this->assertSame( $json, (string)$ret );
97 FormatJson
::encode( $expect, true, FormatJson
::ALL_OK
),
102 MWRestrictions
::newFromJson( $json );
103 $this->fail( 'Expected exception not thrown' );
104 } catch ( InvalidArgumentException
$ex ) {
105 $this->assertTrue( true );
110 public static function provideJson() {
113 '{"IPAddresses":[]}',
114 [ 'IPAddresses' => [] ]
117 '{"IPAddresses":["127.0.0.1/32"]}',
118 [ 'IPAddresses' => [ '127.0.0.1/32' ] ]
121 '{"IPAddresses":["256.0.0.1/32"]}',
122 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
125 '{"IPAddresses":"127.0.0.1/32"}',
126 new InvalidArgumentException( 'IPAddresses is not an array' )
130 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
133 '{"foo":"bar","bar":42}',
134 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
138 new InvalidArgumentException( 'Invalid restrictions JSON' )
142 new InvalidArgumentException( 'Invalid restrictions JSON' )
148 * @covers MWRestrictions::checkIP
149 * @dataProvider provideCheckIP
153 public function testCheckIP( $ip, $pass ) {
154 $this->assertSame( $pass, self
::$restrictionsForChecks->checkIP( $ip ) );
157 public static function provideCheckIP() {
159 [ '10.0.0.1', true ],
160 [ '172.16.0.0', true ],
161 [ '192.0.2.1', false ],
162 [ '2001:db8:1::', true ],
163 [ '2001:0db8:0000:0000:0000:0000:0000:0000', true ],
164 [ '2001:0DB8:8000::', false ],
169 * @covers MWRestrictions::check
170 * @dataProvider provideCheck
171 * @param WebRequest $request
172 * @param Status $expect
174 public function testCheck( $request, $expect ) {
175 $this->assertEquals( $expect, self
::$restrictionsForChecks->check( $request ) );
178 public function provideCheck() {
181 $mockBuilder = $this->getMockBuilder( 'FauxRequest' )
182 ->setMethods( [ 'getIP' ] );
184 foreach ( self
::provideCheckIP() as $checkIP ) {
186 $request = $mockBuilder->getMock();
188 $request->expects( $this->any() )->method( 'getIP' )
189 ->will( $this->returnValue( $checkIP[0] ) );
190 $ok['ip'] = $checkIP[1];
192 /* If we ever add more restrictions, add nested for loops here:
193 * foreach ( self::provideCheckFoo() as $checkFoo ) {
194 * $request->expects( $this->any() )->method( 'getFoo' )
195 * ->will( $this->returnValue( $checkFoo[0] );
196 * $ok['foo'] = $checkFoo[1];
198 * foreach ( self::provideCheckBar() as $checkBar ) {
199 * $request->expects( $this->any() )->method( 'getBar' )
200 * ->will( $this->returnValue( $checkBar[0] );
201 * $ok['bar'] = $checkBar[1];
208 $status = Status
::newGood();
209 $status->setResult( $ok === array_filter( $ok ), $ok );
210 $ret[] = [ $request, $status ];