| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Auth; |
| 4 |
|
| 5 |
use MediaWiki\Auth\AuthManager; |
| 6 |
use MediaWiki\Auth\PasswordAuthenticationRequest; |
| 7 |
use MediaWiki\Message\Message; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class PasswordAuthenticationRequestTest extends AuthenticationRequestTestCase { |
| 14 |
|
| 15 |
protected function getInstance( array $args = [] ) { |
| 16 |
$ret = new PasswordAuthenticationRequest(); |
| 17 |
$ret->action = $args[0]; |
| 18 |
return $ret; |
| 19 |
} |
| 20 |
|
| 21 |
public static function provideGetFieldInfo() { |
| 22 |
return [ |
| 23 |
[ [ AuthManager::ACTION_LOGIN ] ], |
| 24 |
[ [ AuthManager::ACTION_CREATE ] ], |
| 25 |
[ [ AuthManager::ACTION_CHANGE ] ], |
| 26 |
[ [ AuthManager::ACTION_REMOVE ] ], |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function testGetFieldInfo2() { |
| 31 |
$info = []; |
| 32 |
foreach ( [ |
| 33 |
AuthManager::ACTION_LOGIN, |
| 34 |
AuthManager::ACTION_CREATE, |
| 35 |
AuthManager::ACTION_CHANGE, |
| 36 |
AuthManager::ACTION_REMOVE, |
| 37 |
] as $action ) { |
| 38 |
$req = new PasswordAuthenticationRequest(); |
| 39 |
$req->action = $action; |
| 40 |
$info[$action] = $req->getFieldInfo(); |
| 41 |
} |
| 42 |
|
| 43 |
$this->assertSame( [], $info[AuthManager::ACTION_REMOVE], 'No data needed to remove' ); |
| 44 |
|
| 45 |
$this->assertArrayNotHasKey( 'retype', $info[AuthManager::ACTION_LOGIN], |
| 46 |
'No need to retype password on login' ); |
| 47 |
$this->assertArrayHasKey( 'retype', $info[AuthManager::ACTION_CREATE], |
| 48 |
'Need to retype when creating new password' ); |
| 49 |
$this->assertArrayHasKey( 'retype', $info[AuthManager::ACTION_CHANGE], |
| 50 |
'Need to retype when changing password' ); |
| 51 |
|
| 52 |
$this->assertNotEquals( |
| 53 |
$info[AuthManager::ACTION_LOGIN]['password']['label'], |
| 54 |
$info[AuthManager::ACTION_CHANGE]['password']['label'], |
| 55 |
'Password field for change is differentiated from login' |
| 56 |
); |
| 57 |
$this->assertNotEquals( |
| 58 |
$info[AuthManager::ACTION_CREATE]['password']['label'], |
| 59 |
$info[AuthManager::ACTION_CHANGE]['password']['label'], |
| 60 |
'Password field for change is differentiated from create' |
| 61 |
); |
| 62 |
$this->assertNotEquals( |
| 63 |
$info[AuthManager::ACTION_CREATE]['retype']['label'], |
| 64 |
$info[AuthManager::ACTION_CHANGE]['retype']['label'], |
| 65 |
'Retype field for change is differentiated from create' |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
public static function provideLoadFromSubmission() { |
| 70 |
return [ |
| 71 |
'Empty request, login' => [ |
| 72 |
[ AuthManager::ACTION_LOGIN ], |
| 73 |
[], |
| 74 |
false, |
| 75 |
], |
| 76 |
'Empty request, change' => [ |
| 77 |
[ AuthManager::ACTION_CHANGE ], |
| 78 |
[], |
| 79 |
false, |
| 80 |
], |
| 81 |
'Empty request, remove' => [ |
| 82 |
[ AuthManager::ACTION_REMOVE ], |
| 83 |
[], |
| 84 |
false, |
| 85 |
], |
| 86 |
'Username + password, login' => [ |
| 87 |
[ AuthManager::ACTION_LOGIN ], |
| 88 |
$data = [ 'username' => 'User', 'password' => 'Bar' ], |
| 89 |
$data + [ 'action' => AuthManager::ACTION_LOGIN ], |
| 90 |
], |
| 91 |
'Username + password, change' => [ |
| 92 |
[ AuthManager::ACTION_CHANGE ], |
| 93 |
[ 'username' => 'User', 'password' => 'Bar' ], |
| 94 |
false, |
| 95 |
], |
| 96 |
'Username + password + retype' => [ |
| 97 |
[ AuthManager::ACTION_CHANGE ], |
| 98 |
[ 'username' => 'User', 'password' => 'Bar', 'retype' => 'baz' ], |
| 99 |
[ 'password' => 'Bar', 'retype' => 'baz', 'action' => AuthManager::ACTION_CHANGE ], |
| 100 |
], |
| 101 |
'Username empty, login' => [ |
| 102 |
[ AuthManager::ACTION_LOGIN ], |
| 103 |
[ 'username' => '', 'password' => 'Bar' ], |
| 104 |
false, |
| 105 |
], |
| 106 |
'Username empty, change' => [ |
| 107 |
[ AuthManager::ACTION_CHANGE ], |
| 108 |
[ 'username' => '', 'password' => 'Bar', 'retype' => 'baz' ], |
| 109 |
[ 'password' => 'Bar', 'retype' => 'baz', 'action' => AuthManager::ACTION_CHANGE ], |
| 110 |
], |
| 111 |
'Password empty, login' => [ |
| 112 |
[ AuthManager::ACTION_LOGIN ], |
| 113 |
[ 'username' => 'User', 'password' => '' ], |
| 114 |
false, |
| 115 |
], |
| 116 |
'Password empty, login, with retype' => [ |
| 117 |
[ AuthManager::ACTION_LOGIN ], |
| 118 |
[ 'username' => 'User', 'password' => '', 'retype' => 'baz' ], |
| 119 |
false, |
| 120 |
], |
| 121 |
'Retype empty' => [ |
| 122 |
[ AuthManager::ACTION_CHANGE ], |
| 123 |
[ 'username' => 'User', 'password' => 'Bar', 'retype' => '' ], |
| 124 |
false, |
| 125 |
], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
public function testDescribeCredentials() { |
| 130 |
$username = 'TestDescribeCredentials'; |
| 131 |
$req = new PasswordAuthenticationRequest; |
| 132 |
$req->action = AuthManager::ACTION_LOGIN; |
| 133 |
$req->username = $username; |
| 134 |
$ret = $req->describeCredentials(); |
| 135 |
$this->assertIsArray( $ret ); |
| 136 |
$this->assertArrayHasKey( 'provider', $ret ); |
| 137 |
$this->assertInstanceOf( Message::class, $ret['provider'] ); |
| 138 |
$this->assertSame( 'authmanager-provider-password', $ret['provider']->getKey() ); |
| 139 |
$this->assertArrayHasKey( 'account', $ret ); |
| 140 |
$this->assertInstanceOf( Message::class, $ret['account'] ); |
| 141 |
$this->assertSame( [ $username ], $ret['account']->getParams() ); |
| 142 |
} |
| 143 |
} |