Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / auth / AuthenticationRequestTestCase.php
blobb99bbb689caf5a14b0ca6fd3f31d9d4ca63e5e1d
1 <?php
3 namespace MediaWiki\Tests\Auth;
5 use MediaWiki\Auth\AuthenticationRequest;
6 use MediaWiki\Message\Message;
7 use MediaWikiIntegrationTestCase;
8 use ReflectionMethod;
9 use const E_USER_DEPRECATED;
11 /**
12 * @group AuthManager
14 abstract class AuthenticationRequestTestCase extends MediaWikiIntegrationTestCase {
16 /**
17 * @param array $args
19 * @return AuthenticationRequest
21 abstract protected function getInstance( array $args = [] );
23 /**
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'] ) {
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" );
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;
81 public static function provideGetFieldInfo() {
82 return [
83 [ [] ]
87 /**
88 * @dataProvider provideLoadFromSubmissionStatically
89 * @param array $args
90 * @param array $data
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 );
100 } else {
101 $this->assertFalse( $ret );
105 // abstract public static function provideLoadFromSubmission();
108 * Tempory override to make provideLoadFromSubmission static.
109 * See T332865.
111 final public static function provideLoadFromSubmissionStatically() {
112 $reflectionMethod = new ReflectionMethod( static::class, 'provideLoadFromSubmission' );
113 if ( $reflectionMethod->isStatic() ) {
114 return $reflectionMethod->invoke( null );
117 trigger_error(
118 'overriding provideLoadFromSubmission as an instance method is deprecated. (' .
119 $reflectionMethod->getFileName() . ':' . $reflectionMethod->getEndLine() . ')',
120 E_USER_DEPRECATED
123 return $reflectionMethod->invoke( new static() );
127 /** @deprecated class alias since 1.42 */
128 class_alias( AuthenticationRequestTestCase::class, 'MediaWiki\\Auth\\AuthenticationRequestTestCase' );