API: Force indexes for prop=linkshere|transcludedin|fileusage
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / AuthenticationRequestTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 /**
6 * @group AuthManager
7 * @covers MediaWiki\Auth\AuthenticationRequest
8 */
9 class AuthenticationRequestTest extends \MediaWikiTestCase {
10 protected function setUp() {
11 global $wgDisableAuthManager;
12
13 parent::setUp();
14 if ( $wgDisableAuthManager ) {
15 $this->markTestSkipped( '$wgDisableAuthManager is set' );
16 }
17 }
18
19 public function testBasics() {
20 $mock = $this->getMockForAbstractClass( AuthenticationRequest::class );
21
22 $this->assertSame( get_class( $mock ), $mock->getUniqueId() );
23
24 $this->assertType( 'array', $mock->getMetadata() );
25
26 $ret = $mock->describeCredentials();
27 $this->assertInternalType( 'array', $ret );
28 $this->assertArrayHasKey( 'provider', $ret );
29 $this->assertInstanceOf( 'Message', $ret['provider'] );
30 $this->assertArrayHasKey( 'account', $ret );
31 $this->assertInstanceOf( 'Message', $ret['account'] );
32 }
33
34 public function testLoadRequestsFromSubmission() {
35 $mb = $this->getMockBuilder( AuthenticationRequest::class )
36 ->setMethods( [ 'loadFromSubmission' ] );
37
38 $data = [ 'foo', 'bar' ];
39
40 $req1 = $mb->getMockForAbstractClass();
41 $req1->expects( $this->once() )->method( 'loadFromSubmission' )
42 ->with( $this->identicalTo( $data ) )
43 ->will( $this->returnValue( false ) );
44
45 $req2 = $mb->getMockForAbstractClass();
46 $req2->expects( $this->once() )->method( 'loadFromSubmission' )
47 ->with( $this->identicalTo( $data ) )
48 ->will( $this->returnValue( true ) );
49
50 $this->assertSame(
51 [ $req2 ],
52 AuthenticationRequest::loadRequestsFromSubmission( [ $req1, $req2 ], $data )
53 );
54 }
55
56 public function testGetRequestByClass() {
57 $mb = $this->getMockBuilder(
58 AuthenticationRequest::class, 'AuthenticationRequestTest_AuthenticationRequest2'
59 );
60
61 $reqs = [
62 $this->getMockForAbstractClass(
63 AuthenticationRequest::class, [], 'AuthenticationRequestTest_AuthenticationRequest1'
64 ),
65 $mb->getMockForAbstractClass(),
66 $mb->getMockForAbstractClass(),
67 $this->getMockForAbstractClass(
68 PasswordAuthenticationRequest::class, [],
69 'AuthenticationRequestTest_PasswordAuthenticationRequest'
70 ),
71 ];
72
73 $this->assertNull( AuthenticationRequest::getRequestByClass(
74 $reqs, 'AuthenticationRequestTest_AuthenticationRequest0'
75 ) );
76 $this->assertSame( $reqs[0], AuthenticationRequest::getRequestByClass(
77 $reqs, 'AuthenticationRequestTest_AuthenticationRequest1'
78 ) );
79 $this->assertNull( AuthenticationRequest::getRequestByClass(
80 $reqs, 'AuthenticationRequestTest_AuthenticationRequest2'
81 ) );
82 $this->assertNull( AuthenticationRequest::getRequestByClass(
83 $reqs, PasswordAuthenticationRequest::class
84 ) );
85 $this->assertNull( AuthenticationRequest::getRequestByClass(
86 $reqs, 'ClassThatDoesNotExist'
87 ) );
88
89 $this->assertNull( AuthenticationRequest::getRequestByClass(
90 $reqs, 'AuthenticationRequestTest_AuthenticationRequest0', true
91 ) );
92 $this->assertSame( $reqs[0], AuthenticationRequest::getRequestByClass(
93 $reqs, 'AuthenticationRequestTest_AuthenticationRequest1', true
94 ) );
95 $this->assertNull( AuthenticationRequest::getRequestByClass(
96 $reqs, 'AuthenticationRequestTest_AuthenticationRequest2', true
97 ) );
98 $this->assertSame( $reqs[3], AuthenticationRequest::getRequestByClass(
99 $reqs, PasswordAuthenticationRequest::class, true
100 ) );
101 $this->assertNull( AuthenticationRequest::getRequestByClass(
102 $reqs, 'ClassThatDoesNotExist', true
103 ) );
104 }
105
106 public function testGetUsernameFromRequests() {
107 $mb = $this->getMockBuilder( AuthenticationRequest::class );
108
109 for ( $i = 0; $i < 3; $i++ ) {
110 $req = $mb->getMockForAbstractClass();
111 $req->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [
112 'username' => [
113 'type' => 'string',
114 ],
115 ] ) );
116 $reqs[] = $req;
117 }
118
119 $req = $mb->getMockForAbstractClass();
120 $req->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [] ) );
121 $req->username = 'baz';
122 $reqs[] = $req;
123
124 $this->assertNull( AuthenticationRequest::getUsernameFromRequests( $reqs ) );
125
126 $reqs[1]->username = 'foo';
127 $this->assertSame( 'foo', AuthenticationRequest::getUsernameFromRequests( $reqs ) );
128
129 $reqs[0]->username = 'foo';
130 $reqs[2]->username = 'foo';
131 $this->assertSame( 'foo', AuthenticationRequest::getUsernameFromRequests( $reqs ) );
132
133 $reqs[1]->username = 'bar';
134 try {
135 AuthenticationRequest::getUsernameFromRequests( $reqs );
136 $this->fail( 'Expected exception not thrown' );
137 } catch ( \UnexpectedValueException $ex ) {
138 $this->assertSame(
139 'Conflicting username fields: "bar" from ' .
140 get_class( $reqs[1] ) . '::$username vs. "foo" from ' .
141 get_class( $reqs[0] ) . '::$username',
142 $ex->getMessage()
143 );
144 }
145 }
146
147 public function testMergeFieldInfo() {
148 $msg = wfMessage( 'foo' );
149
150 $req1 = $this->getMock( AuthenticationRequest::class );
151 $req1->required = AuthenticationRequest::REQUIRED;
152 $req1->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [
153 'string1' => [
154 'type' => 'string',
155 'label' => $msg,
156 'help' => $msg,
157 ],
158 'string2' => [
159 'type' => 'string',
160 'label' => $msg,
161 'help' => $msg,
162 ],
163 'optional' => [
164 'type' => 'string',
165 'label' => $msg,
166 'help' => $msg,
167 'optional' => true,
168 ],
169 'select' => [
170 'type' => 'select',
171 'options' => [ 'foo' => $msg, 'baz' => $msg ],
172 'label' => $msg,
173 'help' => $msg,
174 ],
175 ] ) );
176
177 $req2 = $this->getMock( AuthenticationRequest::class );
178 $req2->required = AuthenticationRequest::REQUIRED;
179 $req2->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [
180 'string1' => [
181 'type' => 'string',
182 'label' => $msg,
183 'help' => $msg,
184 ],
185 'string3' => [
186 'type' => 'string',
187 'label' => $msg,
188 'help' => $msg,
189 ],
190 'select' => [
191 'type' => 'select',
192 'options' => [ 'bar' => $msg, 'baz' => $msg ],
193 'label' => $msg,
194 'help' => $msg,
195 ],
196 ] ) );
197
198 $req3 = $this->getMock( AuthenticationRequest::class );
199 $req3->required = AuthenticationRequest::REQUIRED;
200 $req3->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [
201 'string1' => [
202 'type' => 'checkbox',
203 'label' => $msg,
204 'help' => $msg,
205 ],
206 ] ) );
207
208 $req4 = $this->getMock( AuthenticationRequest::class );
209 $req4->required = AuthenticationRequest::REQUIRED;
210 $req4->expects( $this->any() )->method( 'getFieldInfo' )->will( $this->returnValue( [] ) );
211
212 // Basic combining
213
214 $fields = AuthenticationRequest::mergeFieldInfo( [ $req1 ] );
215 $expect = $req1->getFieldInfo();
216 foreach ( $expect as $name => &$options ) {
217 $options['optional'] = !empty( $options['optional'] );
218 }
219 unset( $options );
220 $this->assertEquals( $expect, $fields );
221
222 $fields = AuthenticationRequest::mergeFieldInfo( [ $req1, $req4 ] );
223 $this->assertEquals( $expect, $fields );
224
225 try {
226 AuthenticationRequest::mergeFieldInfo( [ $req1, $req3 ] );
227 $this->fail( 'Expected exception not thrown' );
228 } catch ( \UnexpectedValueException $ex ) {
229 $this->assertSame(
230 'Field type conflict for "string1", "string" vs "checkbox"',
231 $ex->getMessage()
232 );
233 }
234
235 $fields = AuthenticationRequest::mergeFieldInfo( [ $req1, $req2 ] );
236 $expect += $req2->getFieldInfo();
237 $expect['string2']['optional'] = false;
238 $expect['string3']['optional'] = false;
239 $expect['select']['options']['bar'] = $msg;
240 $this->assertEquals( $expect, $fields );
241
242 // Combining with something not required
243
244 $req1->required = AuthenticationRequest::PRIMARY_REQUIRED;
245
246 $fields = AuthenticationRequest::mergeFieldInfo( [ $req1, $req2 ] );
247 $expect += $req2->getFieldInfo();
248 $expect['string1']['optional'] = false;
249 $expect['string3']['optional'] = false;
250 $expect['select']['optional'] = false;
251 $expect['select']['options']['bar'] = $msg;
252 $this->assertEquals( $expect, $fields );
253
254 $req2->required = AuthenticationRequest::PRIMARY_REQUIRED;
255
256 $fields = AuthenticationRequest::mergeFieldInfo( [ $req1, $req2 ] );
257 $expect = $req1->getFieldInfo() + $req2->getFieldInfo();
258 $expect['string1']['optional'] = false;
259 $expect['string2']['optional'] = true;
260 $expect['string3']['optional'] = true;
261 $expect['select']['optional'] = false;
262 $expect['select']['options']['bar'] = $msg;
263 $this->assertEquals( $expect, $fields );
264 }
265
266 /**
267 * @dataProvider provideLoadFromSubmission
268 * @param array $fieldInfo
269 * @param array $data
270 * @param array|bool $expectState
271 */
272 public function testLoadFromSubmission( $fieldInfo, $data, $expectState ) {
273 $mock = $this->getMockForAbstractClass( AuthenticationRequest::class );
274 $mock->expects( $this->any() )->method( 'getFieldInfo' )
275 ->will( $this->returnValue( $fieldInfo ) );
276
277 $ret = $mock->loadFromSubmission( $data );
278 if ( is_array( $expectState ) ) {
279 $this->assertTrue( $ret );
280 $expect = call_user_func( [ get_class( $mock ), '__set_state' ], $expectState );
281 $this->assertEquals( $expect, $mock );
282 } else {
283 $this->assertFalse( $ret );
284 }
285 }
286
287 public static function provideLoadFromSubmission() {
288 return [
289 'No fields' => [
290 [],
291 $data = [ 'foo' => 'bar' ],
292 false
293 ],
294
295 'Simple field' => [
296 [
297 'field' => [
298 'type' => 'string',
299 ],
300 ],
301 $data = [ 'field' => 'string!' ],
302 $data
303 ],
304 'Simple field, not supplied' => [
305 [
306 'field' => [
307 'type' => 'string',
308 ],
309 ],
310 [],
311 false
312 ],
313 'Simple field, empty' => [
314 [
315 'field' => [
316 'type' => 'string',
317 ],
318 ],
319 [ 'field' => '' ],
320 false
321 ],
322 'Simple field, optional, not supplied' => [
323 [
324 'field' => [
325 'type' => 'string',
326 'optional' => true,
327 ],
328 ],
329 [],
330 false
331 ],
332 'Simple field, optional, empty' => [
333 [
334 'field' => [
335 'type' => 'string',
336 'optional' => true,
337 ],
338 ],
339 $data = [ 'field' => '' ],
340 $data
341 ],
342
343 'Checkbox, checked' => [
344 [
345 'check' => [
346 'type' => 'checkbox',
347 ],
348 ],
349 [ 'check' => '' ],
350 [ 'check' => true ]
351 ],
352 'Checkbox, unchecked' => [
353 [
354 'check' => [
355 'type' => 'checkbox',
356 ],
357 ],
358 [],
359 false
360 ],
361 'Checkbox, optional, unchecked' => [
362 [
363 'check' => [
364 'type' => 'checkbox',
365 'optional' => true,
366 ],
367 ],
368 [],
369 [ 'check' => false ]
370 ],
371
372 'Button, used' => [
373 [
374 'push' => [
375 'type' => 'button',
376 ],
377 ],
378 [ 'push' => '' ],
379 [ 'push' => true ]
380 ],
381 'Button, unused' => [
382 [
383 'push' => [
384 'type' => 'button',
385 ],
386 ],
387 [],
388 false
389 ],
390 'Button, optional, unused' => [
391 [
392 'push' => [
393 'type' => 'button',
394 'optional' => true,
395 ],
396 ],
397 [],
398 [ 'push' => false ]
399 ],
400 'Button, image-style' => [
401 [
402 'push' => [
403 'type' => 'button',
404 ],
405 ],
406 [ 'push_x' => 0, 'push_y' => 0 ],
407 [ 'push' => true ]
408 ],
409
410 'Select' => [
411 [
412 'choose' => [
413 'type' => 'select',
414 'options' => [
415 'foo' => wfMessage( 'mainpage' ),
416 'bar' => wfMessage( 'mainpage' ),
417 ],
418 ],
419 ],
420 $data = [ 'choose' => 'foo' ],
421 $data
422 ],
423 'Select, invalid choice' => [
424 [
425 'choose' => [
426 'type' => 'select',
427 'options' => [
428 'foo' => wfMessage( 'mainpage' ),
429 'bar' => wfMessage( 'mainpage' ),
430 ],
431 ],
432 ],
433 $data = [ 'choose' => 'baz' ],
434 false
435 ],
436 'Multiselect (2)' => [
437 [
438 'choose' => [
439 'type' => 'multiselect',
440 'options' => [
441 'foo' => wfMessage( 'mainpage' ),
442 'bar' => wfMessage( 'mainpage' ),
443 ],
444 ],
445 ],
446 $data = [ 'choose' => [ 'foo', 'bar' ] ],
447 $data
448 ],
449 'Multiselect (1)' => [
450 [
451 'choose' => [
452 'type' => 'multiselect',
453 'options' => [
454 'foo' => wfMessage( 'mainpage' ),
455 'bar' => wfMessage( 'mainpage' ),
456 ],
457 ],
458 ],
459 $data = [ 'choose' => [ 'bar' ] ],
460 $data
461 ],
462 'Multiselect, string for some reason' => [
463 [
464 'choose' => [
465 'type' => 'multiselect',
466 'options' => [
467 'foo' => wfMessage( 'mainpage' ),
468 'bar' => wfMessage( 'mainpage' ),
469 ],
470 ],
471 ],
472 [ 'choose' => 'foo' ],
473 [ 'choose' => [ 'foo' ] ]
474 ],
475 'Multiselect, invalid choice' => [
476 [
477 'choose' => [
478 'type' => 'multiselect',
479 'options' => [
480 'foo' => wfMessage( 'mainpage' ),
481 'bar' => wfMessage( 'mainpage' ),
482 ],
483 ],
484 ],
485 [ 'choose' => [ 'foo', 'baz' ] ],
486 false
487 ],
488 'Multiselect, empty' => [
489 [
490 'choose' => [
491 'type' => 'multiselect',
492 'options' => [
493 'foo' => wfMessage( 'mainpage' ),
494 'bar' => wfMessage( 'mainpage' ),
495 ],
496 ],
497 ],
498 [ 'choose' => [] ],
499 false
500 ],
501 'Multiselect, optional, nothing submitted' => [
502 [
503 'choose' => [
504 'type' => 'multiselect',
505 'options' => [
506 'foo' => wfMessage( 'mainpage' ),
507 'bar' => wfMessage( 'mainpage' ),
508 ],
509 'optional' => true,
510 ],
511 ],
512 [],
513 [ 'choose' => [] ]
514 ],
515 ];
516 }
517 }