Merge "Add grant for access to private information"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / AuthenticationRequestTestCase.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 /**
6 * @group AuthManager
7 */
8 abstract class AuthenticationRequestTestCase extends \MediaWikiTestCase {
9 abstract protected function getInstance( array $args = [] );
10
11 /**
12 * @dataProvider provideGetFieldInfo
13 */
14 public function testGetFieldInfo( array $args ) {
15 $info = $this->getInstance( $args )->getFieldInfo();
16 $this->assertType( 'array', $info );
17
18 foreach ( $info as $field => $data ) {
19 $this->assertType( 'array', $data, "Field $field" );
20 $this->assertArrayHasKey( 'type', $data, "Field $field" );
21 $this->assertArrayHasKey( 'label', $data, "Field $field" );
22 $this->assertInstanceOf( 'Message', $data['label'], "Field $field, label" );
23
24 if ( $data['type'] !== 'null' ) {
25 $this->assertArrayHasKey( 'help', $data, "Field $field" );
26 $this->assertInstanceOf( 'Message', $data['help'], "Field $field, help" );
27 }
28
29 if ( isset( $data['optional'] ) ) {
30 $this->assertType( 'bool', $data['optional'], "Field $field, optional" );
31 }
32 if ( isset( $data['image'] ) ) {
33 $this->assertType( 'string', $data['image'], "Field $field, image" );
34 }
35
36 switch ( $data['type'] ) {
37 case 'string':
38 case 'password':
39 case 'hidden':
40 break;
41 case 'select':
42 case 'multiselect':
43 $this->assertArrayHasKey( 'options', $data, "Field $field" );
44 $this->assertType( 'array', $data['options'], "Field $field, options" );
45 foreach ( $data['options'] as $val => $msg ) {
46 $this->assertInstanceOf( 'Message', $msg, "Field $field, option $val" );
47 }
48 break;
49 case 'checkbox':
50 break;
51 case 'button':
52 break;
53 case 'null':
54 break;
55 default:
56 $this->fail( "Field $field, unknown type " . $data['type'] );
57 break;
58 }
59 }
60 }
61
62 public static function provideGetFieldInfo() {
63 return [
64 [ [] ]
65 ];
66 }
67
68 /**
69 * @dataProvider provideLoadFromSubmission
70 * @param array $args
71 * @param array $data
72 * @param array|bool $expectState
73 */
74 public function testLoadFromSubmission( array $args, array $data, $expectState ) {
75 $instance = $this->getInstance( $args );
76 $ret = $instance->loadFromSubmission( $data );
77 if ( is_array( $expectState ) ) {
78 $this->assertTrue( $ret );
79 $expect = call_user_func( [ get_class( $instance ), '__set_state' ], $expectState );
80 $this->assertEquals( $expect, $instance );
81 } else {
82 $this->assertFalse( $ret );
83 }
84 }
85
86 abstract public function provideLoadFromSubmission();
87 }