3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\MainConfigNames
;
7 use MediaWiki\User\BotPassword
;
10 * @covers \CreateBotPassword
14 class CreateBotPasswordTest
extends MaintenanceBaseTestCase
{
16 protected function getMaintenanceClass() {
17 return CreateBotPassword
::class;
20 public function testExecuteForShowGrants() {
21 $this->overrideConfigValue( MainConfigNames
::GrantPermissions
, [
24 'importupload' => true,
27 $this->maintenance
->setOption( 'showgrants', 1 );
28 $this->maintenance
->execute();
29 $this->expectOutputString(
30 str_pad( 'GRANT', 20 ) . " DESCRIPTION\n" .
31 str_pad( 'import', 20 ) . " Import pages from other wikis\n"
35 /** @dataProvider provideExecuteForFatalError */
36 public function testExecuteForFatalError( $args, $options, $expectedOutputRegex ) {
37 foreach ( $args as $name => $value ) {
38 $this->maintenance
->setArg( $name, $value );
40 foreach ( $options as $name => $value ) {
41 $this->maintenance
->setOption( $name, $value );
43 $this->expectCallToFatalError();
44 $this->expectOutputRegex( $expectedOutputRegex );
45 $this->maintenance
->execute();
48 public static function provideExecuteForFatalError() {
50 'Invalid grants provided' => [
52 [ 'grants' => 'invalidgrant1234', 'appid' => 'abcdef' ],
53 '/These grants are invalid: invalidgrant1234/',
55 'Non-existing user provided' => [
57 [ 'grants' => 'import', 'appid' => 'abcdef' ],
58 '/Cannot create bot password for non-existent user/',
60 'No arguments or options provided' => [
61 [], [], "/Argument <user> required!\nParam appid required!\nParam grants required!/",
66 public function testExecuteWhenBotPasswordToShort() {
67 $this->testExecuteForFatalError(
68 [ 'user' => $this->getTestUser()->getUserIdentity()->getName(), 'password' => 'abc' ],
69 [ 'grants' => 'import', 'appid' => 'abc' ],
70 '/Bot passwords must have at least ' . BotPassword
::PASSWORD_MINLENGTH
.
71 ' characters. Given password is 3 characters/'
75 public function testExecuteWhenAppIdTooLong() {
76 $this->testExecuteForFatalError(
77 [ 'user' => $this->getTestUser()->getUserIdentity()->getName() ],
78 [ 'grants' => 'import', 'appid' => str_repeat( 'abc', 100 ) ],
79 '/Bot password creation failed/'
83 public function testExecuteWhenAppIdAlreadyExists() {
84 $this->overrideConfigValue( MainConfigNames
::CentralIdLookupProvider
, 'local' );
85 $username = $this->getMutableTestUser()->getUserIdentity()->getName();
86 // Get an existing bot password
87 $bp = BotPassword
::newUnsaved( [
88 'username' => $username,
90 'grants' => [ 'import', 'editpage' ]
92 $bp->save( 'insert' );
93 // Call the maintenance script with the same appId used to create the bot password above.
94 $this->testExecuteForFatalError(
95 [ 'user' => $username ],
96 [ 'grants' => 'import', 'appid' => 'abc' ],
97 '/Bot password creation failed. Does this appid already exist for the user perhaps?/'
101 public function testExecuteForSuccess() {
102 $this->overrideConfigValue( MainConfigNames
::CentralIdLookupProvider
, 'local' );
103 $username = $this->getMutableTestUser()->getUserIdentity()->getName();
104 // Set the valid arguments and options
105 $this->maintenance
->setArg( 'user', $username );
106 $this->maintenance
->setOption( 'grants', 'import,editpage' );
107 $this->maintenance
->setOption( 'appid', 'abcdef' );
108 // Run the maintenance script
109 $this->maintenance
->execute();
110 $this->expectOutputRegex( "/Success[\s\S]*Log in using username.*$username@abcdef/" );