]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/utils/MWRestrictionsTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / utils / MWRestrictionsTest.php
1 <?php
2 class MWRestrictionsTest extends PHPUnit_Framework_TestCase {
3
4         protected static $restrictionsForChecks;
5
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',
12                         ]
13                 ] );
14         }
15
16         /**
17          * @covers MWRestrictions::newDefault
18          * @covers MWRestrictions::__construct
19          */
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()
26                 );
27         }
28
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.
38          */
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 );
50                         }
51                 }
52         }
53
54         public static function provideArray() {
55                 return [
56                         [ [ 'IPAddresses' => [] ], true ],
57                         [ [ 'IPAddresses' => [ '127.0.0.1/32' ] ], true ],
58                         [
59                                 [ 'IPAddresses' => [ '256.0.0.1/32' ] ],
60                                 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
61                         ],
62                         [
63                                 [ 'IPAddresses' => '127.0.0.1/32' ],
64                                 new InvalidArgumentException( 'IPAddresses is not an array' )
65                         ],
66                         [
67                                 [],
68                                 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
69                         ],
70                         [
71                                 [ 'foo' => 'bar', 'bar' => 42 ],
72                                 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
73                         ],
74                 ];
75         }
76
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
86          */
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() );
92
93                         $this->assertSame( $json, $ret->toJson( false ) );
94                         $this->assertSame( $json, (string)$ret );
95
96                         $this->assertSame(
97                                 FormatJson::encode( $expect, true, FormatJson::ALL_OK ),
98                                 $ret->toJson( true )
99                         );
100                 } else {
101                         try {
102                                 MWRestrictions::newFromJson( $json );
103                                 $this->fail( 'Expected exception not thrown' );
104                         } catch ( InvalidArgumentException $ex ) {
105                                 $this->assertTrue( true );
106                         }
107                 }
108         }
109
110         public static function provideJson() {
111                 return [
112                         [
113                                 '{"IPAddresses":[]}',
114                                 [ 'IPAddresses' => [] ]
115                         ],
116                         [
117                                 '{"IPAddresses":["127.0.0.1/32"]}',
118                                 [ 'IPAddresses' => [ '127.0.0.1/32' ] ]
119                         ],
120                         [
121                                 '{"IPAddresses":["256.0.0.1/32"]}',
122                                 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
123                         ],
124                         [
125                                 '{"IPAddresses":"127.0.0.1/32"}',
126                                 new InvalidArgumentException( 'IPAddresses is not an array' )
127                         ],
128                         [
129                                 '{}',
130                                 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
131                         ],
132                         [
133                                 '{"foo":"bar","bar":42}',
134                                 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
135                         ],
136                         [
137                                 '{"IPAddresses":[]',
138                                 new InvalidArgumentException( 'Invalid restrictions JSON' )
139                         ],
140                         [
141                                 '"IPAddresses"',
142                                 new InvalidArgumentException( 'Invalid restrictions JSON' )
143                         ],
144                 ];
145         }
146
147         /**
148          * @covers MWRestrictions::checkIP
149          * @dataProvider provideCheckIP
150          * @param string $ip
151          * @param bool $pass
152          */
153         public function testCheckIP( $ip, $pass ) {
154                 $this->assertSame( $pass, self::$restrictionsForChecks->checkIP( $ip ) );
155         }
156
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 ],
165                 ];
166         }
167
168         /**
169          * @covers MWRestrictions::check
170          * @dataProvider provideCheck
171          * @param WebRequest $request
172          * @param Status $expect
173          */
174         public function testCheck( $request, $expect ) {
175                 $this->assertEquals( $expect, self::$restrictionsForChecks->check( $request ) );
176         }
177
178         public function provideCheck() {
179                 $ret = [];
180
181                 $mockBuilder = $this->getMockBuilder( 'FauxRequest' )
182                         ->setMethods( [ 'getIP' ] );
183
184                 foreach ( self::provideCheckIP() as $checkIP ) {
185                         $ok = [];
186                         $request = $mockBuilder->getMock();
187
188                         $request->expects( $this->any() )->method( 'getIP' )
189                                 ->will( $this->returnValue( $checkIP[0] ) );
190                         $ok['ip'] = $checkIP[1];
191
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];
197                          *
198                          *      foreach ( self::provideCheckBar() as $checkBar ) {
199                          *          $request->expects( $this->any() )->method( 'getBar' )
200                          *              ->will( $this->returnValue( $checkBar[0] );
201                          *          $ok['bar'] = $checkBar[1];
202                          *
203                          *          // etc.
204                          *      }
205                          *  }
206                          */
207
208                         $status = Status::newGood();
209                         $status->setResult( $ok === array_filter( $ok ), $ok );
210                         $ret[] = [ $request, $status ];
211                 }
212
213                 return $ret;
214         }
215 }