Merge "Allow resources to be salvaged across service resets."
[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 protected function setUp() {
10 global $wgDisableAuthManager;
11
12 parent::setUp();
13 if ( $wgDisableAuthManager ) {
14 $this->markTestSkipped( '$wgDisableAuthManager is set' );
15 }
16 }
17
18 abstract protected function getInstance( array $args = [] );
19
20 /**
21 * @dataProvider provideGetFieldInfo
22 */
23 public function testGetFieldInfo( array $args ) {
24 $info = $this->getInstance( $args )->getFieldInfo();
25 $this->assertType( 'array', $info );
26
27 foreach ( $info as $field => $data ) {
28 $this->assertType( 'array', $data, "Field $field" );
29 $this->assertArrayHasKey( 'type', $data, "Field $field" );
30 $this->assertArrayHasKey( 'label', $data, "Field $field" );
31 $this->assertInstanceOf( 'Message', $data['label'], "Field $field, label" );
32
33 if ( $data['type'] !== 'null' ) {
34 $this->assertArrayHasKey( 'help', $data, "Field $field" );
35 $this->assertInstanceOf( 'Message', $data['help'], "Field $field, help" );
36 }
37
38 if ( isset( $data['optional'] ) ) {
39 $this->assertType( 'bool', $data['optional'], "Field $field, optional" );
40 }
41 if ( isset( $data['image'] ) ) {
42 $this->assertType( 'string', $data['image'], "Field $field, image" );
43 }
44
45 switch ( $data['type'] ) {
46 case 'string':
47 case 'password':
48 case 'hidden':
49 break;
50 case 'select':
51 case 'multiselect':
52 $this->assertArrayHasKey( 'options', $data, "Field $field" );
53 $this->assertType( 'array', $data['options'], "Field $field, options" );
54 foreach ( $data['options'] as $val => $msg ) {
55 $this->assertInstanceOf( 'Message', $msg, "Field $field, option $val" );
56 }
57 break;
58 case 'checkbox':
59 break;
60 case 'button':
61 break;
62 case 'null':
63 break;
64 default:
65 $this->fail( "Field $field, unknown type " . $data['type'] );
66 break;
67 }
68 }
69 }
70
71 public static function provideGetFieldInfo() {
72 return [
73 [ [] ]
74 ];
75 }
76
77 /**
78 * @dataProvider provideLoadFromSubmission
79 * @param array $args
80 * @param array $data
81 * @param array|bool $expectState
82 */
83 public function testLoadFromSubmission( array $args, array $data, $expectState ) {
84 $instance = $this->getInstance( $args );
85 $ret = $instance->loadFromSubmission( $data );
86 if ( is_array( $expectState ) ) {
87 $this->assertTrue( $ret );
88 $expect = call_user_func( [ get_class( $instance ), '__set_state' ], $expectState );
89 $this->assertEquals( $expect, $instance );
90 } else {
91 $this->assertFalse( $ret );
92 }
93 }
94
95 abstract public function provideLoadFromSubmission();
96 }