Merge "Allow easy suppression of multiple deleted revs"
[lhc/web/wiklou.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( array(
8 'IPAddresses' => array(
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 array(
56 array( array( 'IPAddresses' => array() ), true ),
57 array( array( 'IPAddresses' => array( '127.0.0.1/32' ) ), true ),
58 array(
59 array( 'IPAddresses' => array( '256.0.0.1/32' ) ),
60 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
61 ),
62 array(
63 array( 'IPAddresses' => '127.0.0.1/32' ),
64 new InvalidArgumentException( 'IPAddresses is not an array' )
65 ),
66 array(
67 array(),
68 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
69 ),
70 array(
71 array( '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 array(
112 array(
113 '{"IPAddresses":[]}',
114 array( 'IPAddresses' => array() )
115 ),
116 array(
117 '{"IPAddresses":["127.0.0.1/32"]}',
118 array( 'IPAddresses' => array( '127.0.0.1/32' ) )
119 ),
120 array(
121 '{"IPAddresses":["256.0.0.1/32"]}',
122 new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
123 ),
124 array(
125 '{"IPAddresses":"127.0.0.1/32"}',
126 new InvalidArgumentException( 'IPAddresses is not an array' )
127 ),
128 array(
129 '{}',
130 new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
131 ),
132 array(
133 '{"foo":"bar","bar":42}',
134 new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
135 ),
136 array(
137 '{"IPAddresses":[]',
138 new InvalidArgumentException( 'Invalid restrictions JSON' )
139 ),
140 array(
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 array(
159 array( '10.0.0.1', true ),
160 array( '172.16.0.0', true ),
161 array( '192.0.2.1', false ),
162 array( '2001:db8:1::', true ),
163 array( '2001:0db8:0000:0000:0000:0000:0000:0000', true ),
164 array( '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 = array();
180
181 $mockBuilder = $this->getMockBuilder( 'FauxRequest' )
182 ->setMethods( array( 'getIP' ) );
183
184 foreach ( self::provideCheckIP() as $checkIP ) {
185 $ok = array();
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[] = array( $request, $status );
211 }
212
213 return $ret;
214 }
215 }