Debian Codegraph @

tests/phpunit/includes/auth/AuthenticationRequestTestCase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Auth;
4
5 use MediaWiki\Auth\AuthenticationRequest;
6 use MediaWiki\Message\Message;
7 use MediaWikiIntegrationTestCase;
8 use ReflectionMethod;
9 use const E_USER_DEPRECATED;
10
11 /**
12 * @group AuthManager
13 */
14 abstract class AuthenticationRequestTestCase extends MediaWikiIntegrationTestCase {
15
16 /**
17 * @param array $args
18 *
19 * @return AuthenticationRequest
20 */
21 abstract protected function getInstance( array $args = [] );
22
23 /**
24 * @dataProvider provideGetFieldInfo
25 */
26 public function testGetFieldInfo( array $args ) {
27 $info = $this->getInstance( $args )->getFieldInfo();
28 $this->assertIsArray( $info );
29
30 foreach ( $info as $field => $data ) {
31 $this->assertIsArray( $data, "Field $field" );
32 $this->assertArrayHasKey( 'type', $data, "Field $field" );
33 $this->assertArrayHasKey( 'label', $data, "Field $field" );
34 $this->assertInstanceOf( Message::class, $data['label'], "Field $field, label" );
35
36 if ( $data['type'] !== 'null' ) {
37 $this->assertArrayHasKey( 'help', $data, "Field $field" );
38 $this->assertInstanceOf( Message::class, $data['help'], "Field $field, help" );
39 }
40
41 if ( isset( $data['optional'] ) ) {
42 $this->assertIsBool( $data['optional'], "Field $field, optional" );
43 }
44 if ( isset( $data['image'] ) ) {
45 $this->assertIsString( $data['image'], "Field $field, image" );
46 }
47 if ( isset( $data['sensitive'] ) ) {
48 $this->assertIsBool( $data['sensitive'], "Field $field, sensitive" );
49 }
50 if ( $data['type'] === 'password' ) {
51 $this->assertTrue( !empty( $data['sensitive'] ),
52 "Field $field, password field must be sensitive" );
53 }
54
55 switch ( $data['type'] ) {
56 case 'string':
57 case 'password':
58 case 'hidden':
59 break;
60 case 'select':
61 case 'multiselect':
62 $this->assertArrayHasKey( 'options', $data, "Field $field" );
63 $this->assertIsArray( $data['options'], "Field $field, options" );
64 foreach ( $data['options'] as $val => $msg ) {
65 $this->assertInstanceOf( Message::class, $msg, "Field $field, option $val" );
66 }
67 break;
68 case 'checkbox':
69 break;
70 case 'button':
71 break;
72 case 'null':
73 break;
74 default:
75 $this->fail( "Field $field, unknown type " . $data['type'] );
76 break;
77 }
78 }
79 }
80
81 public static function provideGetFieldInfo() {
82 return [
83 [ [] ]
84 ];
85 }
86
87 /**
88 * @dataProvider provideLoadFromSubmissionStatically
89 * @param array $args
90 * @param array $data
91 * @param array|bool $expectState
92 */
93 public function testLoadFromSubmission( array $args, array $data, $expectState ) {
94 $instance = $this->getInstance( $args );
95 $ret = $instance->loadFromSubmission( $data );
96 if ( is_array( $expectState ) ) {
97 $this->assertTrue( $ret );
98 $expect = $instance::__set_state( $expectState );
99 $this->assertEquals( $expect, $instance );
100 } else {
101 $this->assertFalse( $ret );
102 }
103 }
104
105 // abstract public static function provideLoadFromSubmission();
106
107 /**
108 * Tempory override to make provideLoadFromSubmission static.
109 * See T332865.
110 */
111 final public static function provideLoadFromSubmissionStatically() {
112 $reflectionMethod = new ReflectionMethod( static::class, 'provideLoadFromSubmission' );
113 if ( $reflectionMethod->isStatic() ) {
114 return $reflectionMethod->invoke( null );
115 }
116
117 trigger_error(
118 'overriding provideLoadFromSubmission as an instance method is deprecated. (' .
119 $reflectionMethod->getFileName() . ':' . $reflectionMethod->getEndLine() . ')',
120 E_USER_DEPRECATED
121 );
122
123 return $reflectionMethod->invoke( new static() );
124 }
125 }
126
127 /** @deprecated class alias since 1.42 */
128 class_alias( AuthenticationRequestTestCase::class, 'MediaWiki\\Auth\\AuthenticationRequestTestCase' );