Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / utils / MWRestrictionsTest.php
blobf2ea489b9f35ee713c86c6f6b716bdfaac0be26e
1 <?php
2 class MWRestrictionsTest extends PHPUnit_Framework_TestCase {
4 protected static $restrictionsForChecks;
6 public static function setUpBeforeClass() {
7 self::$restrictionsForChecks = MWRestrictions::newFromArray( [
8 'IPAddresses' => [
9 '10.0.0.0/8',
10 '172.16.0.0/12',
11 '2001:db8::/33',
13 ] );
16 /**
17 * @covers MWRestrictions::newDefault
18 * @covers MWRestrictions::__construct
20 public function testNewDefault() {
21 $ret = MWRestrictions::newDefault();
22 $this->assertInstanceOf( 'MWRestrictions', $ret );
23 $this->assertSame(
24 '{"IPAddresses":["0.0.0.0/0","::/0"]}',
25 $ret->toJson()
29 /**
30 * @covers MWRestrictions::newFromArray
31 * @covers MWRestrictions::__construct
32 * @covers MWRestrictions::loadFromArray
33 * @covers MWRestrictions::toArray
34 * @dataProvider provideArray
35 * @param array $data
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() );
44 } else {
45 try {
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() {
55 return [
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' )
67 [],
68 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
71 [ 'foo' => 'bar', 'bar' => 42 ],
72 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
77 /**
78 * @covers MWRestrictions::newFromJson
79 * @covers MWRestrictions::__construct
80 * @covers MWRestrictions::loadFromArray
81 * @covers MWRestrictions::toJson
82 * @covers MWRestrictions::__toString
83 * @dataProvider provideJson
84 * @param string $json
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 );
96 $this->assertSame(
97 FormatJson::encode( $expect, true, FormatJson::ALL_OK ),
98 $ret->toJson( true )
100 } else {
101 try {
102 MWRestrictions::newFromJson( $json );
103 $this->fail( 'Expected exception not thrown' );
104 } catch ( InvalidArgumentException $ex ) {
105 $this->assertTrue( true );
110 public static function provideJson() {
111 return [
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' )
129 '{}',
130 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
133 '{"foo":"bar","bar":42}',
134 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
137 '{"IPAddresses":[]',
138 new InvalidArgumentException( 'Invalid restrictions JSON' )
141 '"IPAddresses"',
142 new InvalidArgumentException( 'Invalid restrictions JSON' )
148 * @covers MWRestrictions::checkIP
149 * @dataProvider provideCheckIP
150 * @param string $ip
151 * @param bool $pass
153 public function testCheckIP( $ip, $pass ) {
154 $this->assertSame( $pass, self::$restrictionsForChecks->checkIP( $ip ) );
157 public static function provideCheckIP() {
158 return [
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() {
179 $ret = [];
181 $mockBuilder = $this->getMockBuilder( 'FauxRequest' )
182 ->setMethods( [ 'getIP' ] );
184 foreach ( self::provideCheckIP() as $checkIP ) {
185 $ok = [];
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];
203 * // etc.
208 $status = Status::newGood();
209 $status->setResult( $ok === array_filter( $ok ), $ok );
210 $ret[] = [ $request, $status ];
213 return $ret;