3 namespace MediaWiki\Tests\Auth
;
5 use MediaWiki\Auth\AuthenticationRequest
;
6 use MediaWiki\Message\Message
;
7 use MediaWikiIntegrationTestCase
;
9 use const E_USER_DEPRECATED
;
14 abstract class AuthenticationRequestTestCase
extends MediaWikiIntegrationTestCase
{
19 * @return AuthenticationRequest
21 abstract protected function getInstance( array $args = [] );
24 * @dataProvider provideGetFieldInfo
26 public function testGetFieldInfo( array $args ) {
27 $info = $this->getInstance( $args )->getFieldInfo();
28 $this->assertIsArray( $info );
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" );
36 if ( $data['type'] !== 'null' ) {
37 $this->assertArrayHasKey( 'help', $data, "Field $field" );
38 $this->assertInstanceOf( Message
::class, $data['help'], "Field $field, help" );
41 if ( isset( $data['optional'] ) ) {
42 $this->assertIsBool( $data['optional'], "Field $field, optional" );
44 if ( isset( $data['image'] ) ) {
45 $this->assertIsString( $data['image'], "Field $field, image" );
47 if ( isset( $data['sensitive'] ) ) {
48 $this->assertIsBool( $data['sensitive'], "Field $field, sensitive" );
50 if ( $data['type'] === 'password' ) {
51 $this->assertTrue( !empty( $data['sensitive'] ),
52 "Field $field, password field must be sensitive" );
55 switch ( $data['type'] ) {
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" );
75 $this->fail( "Field $field, unknown type " . $data['type'] );
81 public static function provideGetFieldInfo() {
88 * @dataProvider provideLoadFromSubmissionStatically
91 * @param array|bool $expectState
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 );
101 $this->assertFalse( $ret );
105 // abstract public static function provideLoadFromSubmission();
108 * Tempory override to make provideLoadFromSubmission static.
111 final public static function provideLoadFromSubmissionStatically() {
112 $reflectionMethod = new ReflectionMethod( static::class, 'provideLoadFromSubmission' );
113 if ( $reflectionMethod->isStatic() ) {
114 return $reflectionMethod->invoke( null );
118 'overriding provideLoadFromSubmission as an instance method is deprecated. (' .
119 $reflectionMethod->getFileName() . ':' . $reflectionMethod->getEndLine() . ')',
123 return $reflectionMethod->invoke( new static() );
127 /** @deprecated class alias since 1.42 */
128 class_alias( AuthenticationRequestTestCase
::class, 'MediaWiki\\Auth\\AuthenticationRequestTestCase' );